﻿package jakey.SalaryCount;

import jakey.SalaryCount.SQLite.dbHelper;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class BaseDataActivity extends Activity {
	private dbHelper dbHelper;
	private EditText editText_lv1;
	private EditText editText_lv2;
	private EditText editText_lv3;
	private EditText editText_lv4;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.basedata);

		Button btn = (Button)findViewById(R.id.baseData_AddOrUpdateBtn);
		Button clearBtn = (Button)findViewById(R.id.baseData_Clearbtn);
		clearBtn.setOnClickListener(new ClearBtnListener());
		
		editText_lv1 = (EditText)findViewById(R.id.baseData_lv1);
		editText_lv2 = (EditText)findViewById(R.id.baseData_lv2);
		editText_lv3 = (EditText)findViewById(R.id.baseData_lv3);
		editText_lv4 = (EditText)findViewById(R.id.baseData_lv4);

		dbHelper = new dbHelper(this, "salaryCount_db", null);		
		Cursor cursor = dbHelper.select("baseinfo", null, null, null, null, null, null);		
		
		if(cursor.moveToNext()) {
			btn.setText("更新");
			btn.setOnClickListener(new AddOrUpdateBtnListener("u"));
			editText_lv1.setText(cursor.getString(1));
			editText_lv2.setText(cursor.getString(2));
			editText_lv3.setText(cursor.getString(3));
			editText_lv4.setText(cursor.getString(4));
		} else {			
			btn.setText("添加");
			btn.setOnClickListener(new AddOrUpdateBtnListener("i"));
		}

		cursor.close();
		dbHelper.close();
		
	}

	class ClearBtnListener implements OnClickListener {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			editText_lv1.setText("");
			editText_lv2.setText("");
			editText_lv3.setText("");
			editText_lv4.setText("");
		}
		
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}
	
	class AddOrUpdateBtnListener implements OnClickListener {
		private String str;
		
		public AddOrUpdateBtnListener (String str) {
			this.str = str;
		}

		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			String lv1 = editText_lv1.getText().toString();
			String lv2 = editText_lv2.getText().toString();
			String lv3 = editText_lv3.getText().toString();
			String lv4 = editText_lv4.getText().toString();
			
			//非空判断
			if(lv1.equals("") || lv2.equals("") || lv3.equals("") || lv4.equals("")) {
				Toast.makeText(BaseDataActivity.this, "请填写完整", Toast.LENGTH_SHORT).show();
			}
			
			String values = lv1 + "," + lv2 + "," + lv3 + "," + lv4;
			//插入
			if(str == "i") {				
				dbHelper.exec("INSERT INTO baseinfo (lv1,lv2,lv3,lv4) VALUES(" + values + ") ");
			} else {
				//更新
				dbHelper.exec("UPDATE baseinfo SET lv1 = "+ lv1 +", lv2 = "+ lv2 +", lv3 = "+ lv3 +", lv4 = "+ lv4 );				
			}
			dbHelper.close();
			
			Intent intent = new Intent();
			intent.setClass(BaseDataActivity.this, SalaryCountActivity.class);
			BaseDataActivity.this.startActivity(intent);
		}
		
	}

}
